package com.zygh.lz.util;

import cn.hutool.core.collection.CollectionUtil;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.zygh.lz.dao.SignMapper;
import com.zygh.lz.dao.XlevelserviceMapper;
import com.zygh.lz.entity.Sign;
import com.zygh.lz.entity.app.AppGps;
import com.zygh.lz.entity.app.AppTraffic;
import com.zygh.lz.entity.app.AppTrailerInfo;
import com.zygh.lz.service.impl.PatrolrecordServiceImpl;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lee
 */
@Component
public class TreeUtil {
    @Resource
    private XlevelserviceMapper xlevelserviceMapper;
    @Resource
    private SignMapper signMapper;

    public List<Map> buildTree(List<Map<String, Object>> treeNodes) {
        List<Map> trees = new ArrayList<Map>();
        for (Map treeNode : treeNodes) {
            if ("0".equals(String.valueOf(treeNode.get("pid")))) {
                trees.add(findChildren(treeNode, treeNodes));
            }
        }
        return trees;
    }

    public List<Map<String, Object>> buildByRecursive(List<Map<String, Object>> treeNodes, String policeNumber) {
        //查询是否签到，签到修改列表状态
        Sign sign = signMapper.findSignIn(policeNumber, "2");
        List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
        for (Map treeNode : treeNodes) {
            List<Map<String, Object>> xlevelserviceMapperLastDutys = xlevelserviceMapper.findLastDuty(String.valueOf(treeNode.get("id")));
            if (CollectionUtil.isNotEmpty(xlevelserviceMapperLastDutys)) {
                xlevelserviceMapperLastDutys.forEach(stringObjectMap -> {
                    stringObjectMap.put("signState", 0);
                    if (sign != null) {
                        if (Integer.valueOf(stringObjectMap.get("id").toString()) == (sign.getAssignmentid())) {
                            stringObjectMap.put("signState", 1);
                        }
                    }
                });
                treeNode.put("detachment", xlevelserviceMapperLastDutys);


            } else {
                treeNode.put("detachment", new ArrayList<>());
            }
            if ("0".equals(String.valueOf(treeNode.get("pid")))) {
                trees.add(findChildren(treeNode, treeNodes));
            }
        }
        return trees;
    }

    public Map findChildren(Map treeNode, List<Map<String, Object>> treeNodes) {
        for (Map it : treeNodes) {
            if (String.valueOf(treeNode.get("id")).equals(String.valueOf(it.get("pid")))) {
                if (treeNode.get("detachment") == null) {
                    treeNode.put("detachment", new ArrayList<Map>());
                }
                ((List) treeNode.get("detachment")).add(findChildren(it, treeNodes));
            }
        }
        return treeNode;
    }

    public List<Map> buildTreePolice(List<Map<String, Object>> treeNodes) {
        List<Map> trees = new ArrayList<Map>();
        for (Map treeNode : treeNodes) {
            if ("0".equals(String.valueOf(treeNode.get("pid")))) {
                trees.add(findChildrenPolice(treeNode, treeNodes));
            }
        }
        return trees;
    }

    public Map findChildrenPolice(Map treeNode, List<Map<String, Object>> treeNodes) {
        for (Map it : treeNodes) {
            if (String.valueOf(treeNode.get("id")).equals(String.valueOf(it.get("pid")))) {
                Object man = it.getOrDefault("people", 0);
                Object man1 = treeNode.getOrDefault("people", 0);
                treeNode.put("people", Integer.valueOf(String.valueOf(man)) + Integer.valueOf(String.valueOf(man1)));

                Object count = it.getOrDefault("count", 0);
                Object count1 = treeNode.getOrDefault("count", 0);
                treeNode.put("count", Integer.valueOf(String.valueOf(count)) + Integer.valueOf(String.valueOf(count1)));
                if (treeNode.get("detachment") == null) {
                    treeNode.put("detachment", new ArrayList<Map>());
                }
                ((List) treeNode.get("detachment")).add(findChildren(it, treeNodes));
            }
        }
        return treeNode;
    }

    public List<Map> buildChildTree(List<Map<String, Object>> treeNodes) {
        List<Map> trees = new ArrayList<Map>();
        for (Map treeNode : treeNodes) {
            if ("0".equals(String.valueOf(treeNode.get("pid")))) {
                trees.add(findChild(treeNode, treeNodes));
            }
        }
        return trees;
    }

    public Map findChild(Map treeNode, List<Map<String, Object>> treeNodes) {
        for (Map it : treeNodes) {
            if (String.valueOf(treeNode.get("id")).equals(String.valueOf(it.get("pid")))) {
                if (treeNode.get("child") == null) {
                    treeNode.put("child", new ArrayList<Map>());
                }
                ((List) treeNode.get("child")).add(findChildren(it, treeNodes));
            }
        }
        return treeNode;
    }


    //判断你主次干道
    public static void trailerType(List<AppTraffic> appTrafficById, AppTrailerInfo appTrailerInfo) {
        if (appTrafficById.size() > 0) {
            for (AppTraffic appTraffic : appTrafficById) {
                List<AppGps> trajectoryList = new Gson().fromJson(appTraffic.getArea(), new TypeToken<List<AppGps>>() {
                }.getType());
                StringBuilder sb = new StringBuilder();
                for (AppGps trajectory : trajectoryList) {
                    sb.append(trajectory.getX()).append(",").append(trajectory.getY()).append(",");
                }
                sb.delete(sb.length() - 1, sb.length());
                Map<String, double[]> border = DistanceUtil.getBorder(sb.toString());
                boolean result = PatrolrecordServiceImpl.isInPolygon(Double.parseDouble(appTrailerInfo.getLongitude()), Double.parseDouble(appTrailerInfo.getLatitude()), border.get("gpsx"), border.get("gpsy"));
                if (result) {
                    appTrailerInfo.setTrafficType(0);
                }
            }

        }
    }
}
